package personal.gavin.gtweet.widget
{
	import flash.events.EventDispatcher;
	import personal.gavin.utils.DateUtil;
	import personal.gavin.gtweet.event.LoadFinishEvent;
	import personal.gavin.gtweet.event.ProcessXMLFinishEvent;
	
	public class StatusUtil
	{
		public function StatusUtil()
		{
		}
		
		/**
		 * statused dispatcher 
		 */		
		private var _statusesDispatcher:EventDispatcher = new EventDispatcher();
		
		public function getStatusesById(id:Number, callback:Function):void {
			var url:String = Setting.api_url + "statuses/user_timeline.xml?id=" + id;
			getStatusesByUrl(url,callback);
		}
		
		public function getStatusesByName(screen_name:String, callback:Function):void {
			var url:String = Setting.api_url + "statuses/user_timeline.xml?id=" + screen_name;
			getStatusesByUrl(url,callback);
		}
		
		/**
		 * connect api server to get profile data 
		 * @param url profile url
		 */		
		public function getStatusesByUrl(url:String, callback:Function):void {
			this._statusesDispatcher.addEventListener(ProcessXMLFinishEvent.PROCESS_XML_FINISHED, callback);
			var connection:URLRequestHelp = new URLRequestHelp(url);
			connection._eventDispatcher.addEventListener(LoadFinishEvent.LOADFINISHED,doProcessXML);
			connection.load();
		}
		
		private function doProcessXML(net_finish:LoadFinishEvent):void{
			var xml_data:String = net_finish.data;
			var _statuses:Statuses = new Statuses();
			if (net_finish.status != 200){
				_statuses.error = new Error(net_finish.status.toString() + " error statuses");
			} else {
				_statuses = StatusUtil.convertStringtoStatuses(xml_data);
			}
			var _process_event:ProcessXMLFinishEvent = new ProcessXMLFinishEvent(ProcessXMLFinishEvent.PROCESS_XML_FINISHED,_statuses);
			this._statusesDispatcher.dispatchEvent(_process_event);
		}
		
		public static function convertStringtoStatuses(data:String):Statuses {
			var _statuses:Statuses = new Statuses();
			
			if (data == null){
				_statuses.error = new Error("Can't get statuses");
				return _statuses;
			}
			var xml_data:XML = new XML(data);
			trace(xml_data.FlashPlayer.toString());
			_statuses = convertXMLtoStatuses(xml_data);
			return _statuses;
		}

		public static function convertXMLtoStatuses(data:XML):Statuses {
			var _statuses:Statuses = new Statuses();
			
			_statuses.xml_data = data;
			trace(_statuses.xml_data.FlashPlayer.toString());
			
			var error:String = _statuses.xml_data.child("error").toString();
			if ( error != ""){
				_statuses.error = new Error(error);
			} else {
				for each (var axml_status:XML in _statuses.xml_data.status){
					var astatus:Status = convertXMLtoStatus(axml_status);
					_statuses.push(astatus);
				}
			}
			return _statuses;
		}
		
		public static function convertXMLtoStatus(data:XML):Status {
			var _status:Status = new Status();
			_status.xml_data = data;
			
			trace(_status.xml_data.FlashPlayer.toString());
			
			var error:String = _status.xml_data.child("error").toString();
			if ( error != ""){
				_status.error = new Error(error);
			} else {
				_status.id = parseInt(_status.xml_data.child("id").toString());
				_status.text = _status.xml_data.child("text").toString();
				
				_status.source = _status.xml_data.child("source").toString();
				_status.in_reply_to_screen_name = _status.xml_data.child("in_reply_to_screen_name").toString();
				
				_status.in_reply_to_status_id = parseInt(_status.xml_data.child("in_reply_to_status_id").toString());
				_status.in_reply_to_user_id = parseInt(_status.xml_data.child("in_reply_to_user_id").toString());
							
				var time_str:String = _status.xml_data.child("created_at").toString();
				_status.time = DateUtil.timeStringToDate(time_str);
			}
			return _status;
		}

	}
}